#include <time.h>
#include "CRebel.h"

#define IMAGE		"U1.png"
#define REBEL_RELEASE		-1
#define REBEL_MOVE			0
#define REBEL_SURPRISE		1
#define REBEL_TALK			2
#define REBEL_SITDOWN		3
#define REBEL_CRAWL			4
#define REBEL_FLY			5
#define REBEL_THROW			6
#define REBEL_THROW1		7
#define REBEL_TALKFONE		8
#define REBEL_STAND			9
#define REBEL_JUMP			10
#define REBEL_FEAR			11
#define REBEL_DIE			12
#define REBEL_DIE1			13
#define REBEL_DIE2			14
#define REBEL_DIE3			15

#define LEFT			0
#define GROUND			600-180

CRebel::CRebel()
{
}
CRebel::CRebel(LPD3DXSPRITE SurpriteHandler, float Scaling, CQuadTree* q)
{
	m_Quadtree = q;

	m_fframeWidth = 50.0f;
	m_fframeHeight = 50.0f;
	m_fscaling = Scaling;
	m_fverX = 0.0f;
	m_fverY = 0.0f;

	m_nAndr = 350;
	m_nrebIndex = 0;
	m_nposX = 0;
	m_nposY = GROUND;
	m_nId = 2;

	m_brelease = false;
	m_bcheckDie = false;

	m_dwlastUpdate = 0;

	timeBetweenUpdated = 120;

	m_Sprite = new CSprite();

	loadSprite(SurpriteHandler);
	setRect();
	suprise();
}

CRebel::~CRebel()
{
	m_Sprite = NULL;
}

void CRebel::reset()
{
	m_nposX = 0;
	m_nposY = 0;
}

void CRebel::loadSprite(LPD3DXSPRITE ImageHandler)
{
	m_Sprite->CreateSprite(ImageHandler, IMAGE, m_fframeWidth, m_fframeHeight, 31, 30, m_fscaling, D3DCOLOR_XRGB(255, 255, 255));
}
void CRebel::update(int DeltaTime, int moveCame)
{
	m_nAndr -= (int) m_fverX;
	m_nposX = m_nAndr - moveCame;
	m_nposY += (int) m_fverY;
	setRect();

	DWORD TimeNow = GetTickCount();

	if (TimeNow - m_dwlastUpdate > timeBetweenUpdated)
	{
		next();
		m_dwlastUpdate = TimeNow;
	}
}


void CRebel::render()
{
	//if(m_brelease != true)
		m_Sprite->Render(m_nposX, m_nposY);
}

void CRebel::next()
{
	if(m_brelease != true)
		m_Sprite->Next();
	//line 0
	if(m_Sprite->GetIndex() == 12 && m_nrebIndex == REBEL_MOVE)	//set index 
		m_Sprite->SetIndex(0);
	//set index REBEL_SURPRISE
	if(m_Sprite->GetIndex() == 17 && m_nrebIndex == REBEL_SURPRISE)
		talk();

	if(m_Sprite->GetIndex() == 22 && m_nrebIndex == REBEL_TALK)
		m_Sprite->SetIndex(18);

	if(m_Sprite->GetIndex() == 29 && m_nrebIndex == REBEL_SITDOWN)
		m_Sprite->SetIndex(23);
	//line 1
	if(m_Sprite->GetIndex() == 4 && m_nrebIndex == REBEL_CRAWL)
		m_Sprite->SetIndex(0);

	if(m_Sprite->GetIndex() == 14 && m_nrebIndex == REBEL_FLY)
		m_Sprite->SetIndex(5);

	if(m_Sprite->GetIndex() == 27 && m_nrebIndex == REBEL_THROW)
		m_Sprite->SetIndex(15);
	//line 2
	if(m_Sprite->GetIndex() == 17 && m_nrebIndex == REBEL_THROW1)
		m_Sprite->SetIndex(0);

	if(m_Sprite->GetIndex() == 29 && m_nrebIndex == REBEL_TALKFONE)
		m_Sprite->SetIndex(18);
	//line 3
	if(m_Sprite->GetIndex() == 6 && m_nrebIndex == REBEL_STAND)
		m_Sprite->SetIndex(0);

	if(m_Sprite->GetIndex() == 24 && m_nrebIndex == REBEL_JUMP)
		m_Sprite->SetIndex(7);

	if(m_Sprite->GetIndex() == 29 && m_nrebIndex == REBEL_FEAR)
		m_Sprite->SetIndex(25);
	//
	if(m_Sprite->GetIndex() == 15 && m_nrebIndex == REBEL_DIE)
		release();

	if(m_Sprite->GetIndex() == 30 && m_nrebIndex == REBEL_DIE1)
		m_Sprite->SetIndex(16);

	if(m_Sprite->GetIndex() == 12 && m_nrebIndex == REBEL_DIE2)
		m_Sprite->SetIndex(0);

	if(m_Sprite->GetIndex() == 29 && m_nrebIndex == REBEL_DIE3)
		m_Sprite->SetIndex(13);
}

void CRebel::setRect()
{
	//vector<CRebel*> hg;
	m_ObRect = new RECT();
	m_ObRect->left = m_nAndr;
	m_ObRect->right = m_ObRect->left + m_fframeWidth- 20;
	m_ObRect->top = m_nposY;
	m_ObRect->bottom = m_ObRect->top + m_fframeHeight - 5;
}
//line 0
void CRebel::move()
{
	m_nrebIndex = REBEL_MOVE;
	m_Sprite->SetIndex(0);
	m_Sprite->SetSpriteLine(0);
}

void CRebel::suprise()
{
	m_nrebIndex = REBEL_SURPRISE;
	m_Sprite->SetIndex(13);
	m_Sprite->SetSpriteLine(0);
}

void CRebel::talk()
{
	m_nrebIndex = REBEL_TALK;
	m_Sprite->SetIndex(18);
	m_Sprite->SetSpriteLine(0);
}
void CRebel::sitDown()
{
	m_nrebIndex = REBEL_SITDOWN;
	m_Sprite->SetIndex(23);
	m_Sprite->SetSpriteLine(0);
}
//line 1
void CRebel::crawl()
{
	m_nrebIndex = REBEL_CRAWL;
	m_Sprite->SetIndex(0);
	m_Sprite->SetSpriteLine(1);
}

void CRebel::fly()
{
	m_nrebIndex = REBEL_FLY;
	m_Sprite->SetIndex(5);
	m_Sprite->SetSpriteLine(1);
}

void CRebel::throw1()
{
	m_nrebIndex = REBEL_THROW;
	m_Sprite->SetIndex(15);
	m_Sprite->SetSpriteLine(1);
}
//line 2
void CRebel::throw2()
{
	m_nrebIndex = REBEL_THROW1;
	m_Sprite->SetIndex(0);
	m_Sprite->SetSpriteLine(2);
}
 
void CRebel::talkFone()
{
	m_nrebIndex = REBEL_TALKFONE;
	m_Sprite->SetIndex(18);
	m_Sprite->SetSpriteLine(2);
}

void CRebel::stand()
{
	m_nrebIndex = REBEL_STAND;
	m_Sprite->SetIndex(0);
	m_Sprite->SetSpriteLine(3);
}

void CRebel::jump()
{
	m_nrebIndex = REBEL_JUMP;
	m_Sprite->SetIndex(7);
	m_Sprite->SetSpriteLine(3);
}
void CRebel::fear()
{
	m_nrebIndex = REBEL_FEAR;
	m_Sprite->SetIndex(25);
	m_Sprite->SetSpriteLine(3);
}

//
void CRebel::die()
{
	m_fverX = 0.0f;
	m_bcheckDie = true;
	m_nrebIndex = REBEL_DIE;
	m_Sprite->SetIndex(0);
	m_Sprite->SetSpriteLine(7);
}

void CRebel::die1()
{
	m_fverX = 0.0f;
	m_bcheckDie = true;
	m_nrebIndex = REBEL_DIE1;
	m_Sprite->SetIndex(16);
	m_Sprite->SetSpriteLine(7);
}

void CRebel::die2()
{
	m_fverX = 0.0f;
	m_bcheckDie = true;
	m_nrebIndex = REBEL_DIE2;
	m_Sprite->SetIndex(0);
	m_Sprite->SetSpriteLine(8);
}

void CRebel::die3()
{
	m_fverX = 0.0f;
	m_bcheckDie = true;
	m_nrebIndex = REBEL_DIE3;
	m_Sprite->SetIndex(13);
	m_Sprite->SetSpriteLine(8);
}
void CRebel::release()
{
	m_brelease = true;
}